import pygame
import math

# 初始化 Pygame
pygame.init()

# 设置屏幕尺寸
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("愤怒的小鸟")

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)

# 小鸟类
class Bird(pygame.sprite.Sprite):
    def __init__(self, x, y, radius, color):
        super().__init__()
        self.image = pygame.Surface((radius * 2, radius * 2))
        self.image.fill(WHITE)
        self.image.set_colorkey(WHITE)
        pygame.draw.circle(self.image, color, (radius, radius), radius)
        self.rect = self.image.get_rect()
        self.rect.center = (x, y)
        self.vx = 0
        self.vy = 0
        self.gravity = 0.5

    def update(self):
        self.vy += self.gravity
        self.rect.x += self.vx
        self.rect.y += self.vy

        # 边界检测
        if self.rect.bottom > HEIGHT:
            self.rect.bottom = HEIGHT
            self.vy = 0
        if self.rect.left < 0:
            self.rect.left = 0
            self.vx = -self.vx
        if self.rect.right > WIDTH:
            self.rect.right = WIDTH
            self.vx = -self.vx

# 障碍物类
class Obstacle(pygame.sprite.Sprite):
    def __init__(self, x, y, width, height, color):
        super().__init__()
        self.image = pygame.Surface((width, height))
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.topleft = (x, y)

# 创建小鸟和障碍物
bird = Bird(50, HEIGHT - 50, 20, RED)
obstacle = Obstacle(600, HEIGHT - 100, 100, 100, BLACK)

# 创建精灵组
all_sprites = pygame.sprite.Group()
all_sprites.add(bird)
all_sprites.add(obstacle)

# 游戏主循环
clock = pygame.time.Clock()
dragging = False
launch_power = 0
angle = 0

running = True
while running:
    clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                dragging = True
        elif event.type == pygame.MOUSEBUTTONUP:
            if event.button == 1:
                dragging = False
                # 计算发射速度和角度
                dx = pygame.mouse.get_pos()[0] - bird.rect.centerx
                dy = pygame.mouse.get_pos()[1] - bird.rect.centery
                launch_power = math.sqrt(dx ** 2 + dy ** 2) / 10
                angle = math.atan2(dy, dx)
                bird.vx = -launch_power * math.cos(angle)
                bird.vy = -launch_power * math.sin(angle)

    if dragging:
        dx = pygame.mouse.get_pos()[0] - bird.rect.centerx
        dy = pygame.mouse.get_pos()[1] - bird.rect.centery
        launch_power = math.sqrt(dx ** 2 + dy ** 2) / 10
        angle = math.atan2(dy, dx)
        pygame.draw.line(screen, RED, bird.rect.center, pygame.mouse.get_pos(), 3)

    # 更新精灵
    all_sprites.update()

    # 碰撞检测
    if pygame.sprite.collide_rect(bird, obstacle):
        print("碰撞发生！")

    # 绘制背景
    screen.fill(WHITE)

    # 绘制精灵
    all_sprites.draw(screen)

    pygame.display.flip()

pygame.quit()